/**
 * 
 */
package org.mspring.mlog.entity;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.lucene.document.Document;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.mspring.mlog.entity.security.User;
import org.mspring.mlog.search.Searchable;
import org.mspring.platform.cache.service.CacheService;

import com.google.gson.annotations.Expose;

/**
 * @author Gao Youbo
 * @since 2012-7-23
 * @Description
 * @TODO
 */
@Entity
@Table(name = "post")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Post implements Serializable, Searchable {
    /**
     * 
     */
    private static final long serialVersionUID = -425742660561620768L;

    @Expose
    private Long id;
    @Expose
    private String title;
    private String summary;
    private String content;
    private Date createTime;
    private Date modifyTime;
    private User author;
    private String status;
    private String password;
    private String commentStatus;
    private String postIp;
    private Boolean isTop;
    private String sourceUrl;// 来源链接
    private String sourceSite;// 来源站点
    private PostCatalog catalog;

    private Long comments;
    private Long views;

    public Post() {
    }

    public Post(Long id) {
        super();
        this.id = id;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false, length = 30)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "title", unique = false, nullable = false, length = 200)
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Column(name = "summary", columnDefinition = "text")
    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    @Column(name = "content", nullable = false, columnDefinition = "text")
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Column(name = "create_time", length = 100)
    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Column(name = "modify_time", length = 100)
    @Temporal(TemporalType.TIMESTAMP)
    public Date getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }

    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = CacheService.CacheName.LAZY_CACHE_NAME)
    @ManyToOne(fetch = FetchType.EAGER, optional = false, targetEntity = User.class)
    @JoinColumn(name = "post_user")
    public User getAuthor() {
        return author;
    }

    public void setAuthor(User author) {
        this.author = author;
    }

    @Column(name = "post_status", nullable = false, length = 20)
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Column(name = "password", length = 30)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "comment_status", nullable = false, length = 20)
    public String getCommentStatus() {
        return commentStatus;
    }

    public void setCommentStatus(String commentStatus) {
        this.commentStatus = commentStatus;
    }

    @Column(name = "comments", length = 20)
    public Long getComments() {
        return comments;
    }

    public void setComments(Long comments) {
        this.comments = comments;
    }

    @Column(name = "source_url", length = 300)
    public String getSourceUrl() {
        return sourceUrl;
    }

    public void setSourceUrl(String sourceUrl) {
        this.sourceUrl = sourceUrl;
    }

    @Column(name = "source_site", length = 300)
    public String getSourceSite() {
        return sourceSite;
    }

    public void setSourceSite(String sourceSite) {
        this.sourceSite = sourceSite;
    }

    @Column(name = "post_ip", length = 50)
    public String getPostIp() {
        return postIp;
    }

    public void setPostIp(String postIp) {
        this.postIp = postIp;
    }

    @Column(name = "views", length = 20)
    public Long getViews() {
        return views;
    }

    public void setViews(Long views) {
        this.views = views;
    }

    @Column(name = "is_top", length = 2)
    public Boolean getIsTop() {
        return isTop;
    }

    public void setIsTop(Boolean isTop) {
        this.isTop = isTop;
    }

    @ManyToOne(fetch = FetchType.EAGER, optional = true, targetEntity = PostCatalog.class)
    @JoinColumn(name = "catalog")
    public PostCatalog getCatalog() {
        return catalog;
    }

    public void setCatalog(PostCatalog catalog) {
        this.catalog = catalog;
    }

    public static class Status {
        public static final String PUBLISH = "publish"; // 已发布的
        public static final String DRAFT = "draft"; // 草稿
        public static final String TRASH = "trash"; // 废弃的
        public static final List<String> STATUS = Arrays.asList(new String[] { PUBLISH, DRAFT, TRASH });

        public static final Map<String, String> getStatusMap() {
            Map<String, String> map = new HashMap<String, String>();
            map.put(PUBLISH, "已发布");
            map.put(DRAFT, "草稿");
            map.put(TRASH, "回收站");
            return map;
        }
    }

    public static class CommentStatus {
        public static final String OPEN = "open"; // 允许评论
        public static final String CLOSE = "close"; // 关闭评论
        public static final List<String> COMMENT_STATUS = Arrays.asList(new String[] { OPEN, CLOSE });

        public static final Map<String, String> getCommentStatusMap() {
            Map<String, String> map = new HashMap<String, String>();
            map.put(OPEN, "开启");
            map.put(CLOSE, "关闭");
            return map;
        }
    }

    // /////////////////////////////////////////////////////////////////////////
    // Search
    // /////////////////////////////////////////////////////////////////////////

    @Override
    public Long documentId() {
        // TODO Auto-generated method stub
        return id;
    }

    @Override
    public void documentId(Long id) {
        // TODO Auto-generated method stub
        this.id = id;
    }

    @Override
    public List<String> storeFields() {
        // TODO Auto-generated method stub
        return Arrays.asList("id", "title", "content", "createTime");
    }

    @Override
    public List<String> indexFields() {
        // TODO Auto-generated method stub
        return Arrays.asList("id", "title", "summary", "content", "status");
    }

    @Override
    public float boost() {
        // TODO Auto-generated method stub
        return 1.0f;
    }

    @Override
    public Map<String, String> extendStoreDatas() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Map<String, String> extendIndexDatas() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Searchable document2Object(Document doc) {
        // TODO Auto-generated method stub
        if (doc == null) {
            return null;
        }
        String idString = doc.get("id");
        String titleString = doc.get("title");
        String contentString = doc.get("content");
        String createTimeString = doc.get("createTime");
        Post post = new Post();
        post.setId(Long.valueOf(idString));
        post.setTitle(titleString);
        post.setContent(contentString);
        post.setCreateTime(new Date(Long.valueOf(createTimeString)));
        return post;
    }

}
